<template>
  <div class="login-container">
    <!--<div class="header">
      <div class="wrapper">
        <div class="title">
          <span style="display: inline-block; margin-left: 10px">
            疫情数据分析可视化平台
          </span>
        </div>
      </div>
    </div>-->
    <!-- 中间 -->
    <div class="center">
      <div class="wrapper">
        <!-- 表单 -->
        <div class="form">
          <el-form
            ref="loginForm"
            :model="loginForm"
            :rules="loginRules"
            class="login-form"
            autocomplete="on"
            label-position="left"
            size="small"
          >
            <div
              style="
                height: 48px;
                font-size: 18px;
                font-weight: bold;
                line-height: 48px;
                text-align: center;
              "
            >
              欢迎来到苏州市疫情数据分析可视化平台
            </div>
            <div
              style="
                width: 80%;
                height: 1px;
                margin: 0 auto;
                margin-bottom: 1em;
                border-bottom: 1px solid #ccc;
              "
            ></div>
            <el-form-item prop="username">
              <span class="svg-container">
                <svg-icon icon-class="user" />
              </span>
              <el-input
                ref="username"
                v-model="loginForm.username"
                placeholder="用户名"
                type="text"
              />
            </el-form-item>
            <el-tooltip
              v-model="capsTooltip"
              content="Caps lock is On"
              placement="right"
              manual
            >
              <el-form-item prop="password">
                <span class="svg-container">
                  <svg-icon icon-class="password" />
                </span>
                <el-input
                  :key="passwordType"
                  ref="password"
                  v-model="loginForm.password"
                  :type="passwordType"
                  placeholder="密码"
                />
                <span class="show-pwd" @click="showPwd">
                  <svg-icon
                    :icon-class="
                      passwordType === 'password' ? 'eye' : 'eye-open'
                    "
                  />
                </span>
              </el-form-item>
            </el-tooltip>
            <div>
              <el-button
                :loading="loading"
                style="width: 100%"
                @click.native.prevent="handleLogin"
                >登录</el-button
              >
            </div>
          </el-form>
          <!-- 表单 -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import { post } from '@/utils/request'
// import { mapGetters } from 'vuex'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      callback()
    }
    const validatePassword = (rule, value, callback) => {
      callback()
    }
    return {
      visible: false,
      form: {},
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        realname: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted() {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    // 重置表单校验
    clearValidate() {
      this.$refs['registerForm'].clearValidate()
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || '/',
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>

<style lang="scss" scoped>
/* reset element-ui css */
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  //  position: absolute;
  // top: 0;
  // bottom: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  // background-color: #f4f4f4;
  background: url('/public/images/licensed-image.jpg') center center / cover
    no-repeat;
  // flex-direction: column;

  .wrapper {
    width: 90%;
    margin: 0 auto;
  }

  .header {
    position: relative;
    color: #fff;
    // background-color: #008964;
    background-color: #4c84ee;

    .title {
      font-size: 20px;
      font-weight: bold;
      line-height: 60px;
    }
  }

  .center {
    // position: relative;
    // top: 60px;
    box-sizing: border-box;
    width: 500px;
    background-color: #fff;
    background-color: rgb(255 255 255 / 70%);
    border-radius: 10px;
    box-shadow: 0 0 10px #ccc;

    .wrapper {
      width: 100%;
      padding: 2em;
      margin: 0 auto;
      border-radius: 10px;
      backdrop-filter: blur(10px) saturate(0.5);
      backdrop-filter: blur(10px) saturate(0.5);

      input,
      button {
        background-color: rgb(255 255 255 / 65%);
      }
    }
  }
}
</style>
